package com.bigevent.controller;

import com.bigevent.pojo.Result;
import com.bigevent.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> uploadFile(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        //利用UUID保证文件名字唯一，防止发生文件覆盖
        String fileName = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        //将文件内容保存在本地磁盘
//        file.transferTo(new File("D:\\OneDrive\\桌面\\Practice\\uploadFileTest\\" + fileName));
        String URL = AliOssUtil.uploadFile(fileName, file.getInputStream());
        return Result.success(URL);
    }
}
